package edu.example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.*;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;


public class LoginInfoFrame extends JFrame
{
	private Image img;

	//
	private String password;
	private String username;
	
	//Labels
	private JLabel userNameLabel;
	private JLabel passwordLabel;
	private JLabel errorLabel;
	private JLabel bannerLabel;
	private JLabel buttomLabel;
	
	private JTextField userNameField;
	private JPasswordField passwordField;
	
	//Buttons
	private JButton loginButton;
	
	//Panel
	private JPanel entryPanel;
	private JPanel loginPanel;
	
	//Handler
	private loginHandler loginHandler;
	
	private UserManager um;
	
	/**
	 * Constructor
	 */
	public LoginInfoFrame()
	{
		um = new UserManager();
		this.setSize(800, 600);
		this.setResizable(false);
		this.setTitle("Graduate Application Review System ");
		layoutGUI();
	}
	
	public void layoutGUI()
	{	
		// size and locate the dialog at the center of the screen
		// size and locate the dialog at the center of the screen
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
		//Construct GUI components
		userNameLabel = new JLabel("Username: ");
		passwordLabel = new JLabel("Password: ");
		userNameField = new JTextField(10);
		passwordField = new JPasswordField(10);
		
		loginButton = new JButton("Login");
		errorLabel = new JLabel("");
		errorLabel.setPreferredSize(new Dimension(140, 25));
		
		
		entryPanel = new JPanel();
		loginPanel = new JPanel();
		
		bannerLabel = new JLabel();
		bannerLabel.setIcon(new ImageIcon("banner.png"));
		
		buttomLabel = new JLabel();
		buttomLabel.setIcon(new ImageIcon("bottum.png"));
		
		GridBagLayout gbl = new GridBagLayout();
		entryPanel.setLayout(gbl);
		
		addJComponentToContainerUsingGBL(userNameLabel, entryPanel,	0,	0,	1,	1);
		addJComponentToContainerUsingGBL(userNameField, entryPanel,	2,	0,	1,	1);
		addJComponentToContainerUsingGBL(passwordLabel, entryPanel,	0,	1,	1,	1);
		addJComponentToContainerUsingGBL(passwordField, entryPanel,	2,	1,	1,	1);
		addJComponentToContainerUsingGBL(loginPanel, entryPanel,	1,	2,	1,	1);
		
		loginPanel.setLayout(new BorderLayout());
		loginPanel.add(errorLabel, BorderLayout.NORTH);
		loginPanel.add(loginButton, BorderLayout.SOUTH);
		
		this.add(bannerLabel, BorderLayout.NORTH);
		this.add(entryPanel, BorderLayout.CENTER);
		this.add(buttomLabel, BorderLayout.SOUTH);


		
		//Construct handler
		loginHandler = new loginHandler();
		
		//Link component to hander
		loginButton.addActionListener(loginHandler);
		userNameField.addKeyListener(loginHandler);
		passwordField.addKeyListener(loginHandler);
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addJComponentToContainerUsingGBL(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	class loginHandler implements ActionListener, KeyListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			password = passwordField.getText();
			username = userNameField.getText();
			boolean valid = false;
			valid = um.login(username, password);
			
			if(!password.isEmpty() && !username.isEmpty() && valid)
			{
				MainScreenFrame mainScreenFrame = new MainScreenFrame(um);
				mainScreenFrame.setVisible(true);
				LoginInfoFrame.this.setVisible(false);
			}
			else
			{
				errorLabel.setForeground(Color.RED);
				if(password.isEmpty())
				{
					errorLabel.setText("Please Enter Password.");
				}
				else if(username.isEmpty())
				{
					errorLabel.setText("Please Enter Username.");
				}
				else if (!valid){
					errorLabel.setText("Invalid User.");
				}	
			}
		}

		public void keyTyped(KeyEvent e) {}

		public void keyPressed(KeyEvent e) 
		{
			if(e.getKeyChar() == KeyEvent.VK_ENTER)
			{
				password = passwordField.getText();
				username = userNameField.getText();
				boolean valid = um.login(username, password);
				if(!password.isEmpty() && !username.isEmpty() && valid)
				{
					MainScreenFrame mainScreenFrame = new MainScreenFrame(um);
					mainScreenFrame.setVisible(true);
					LoginInfoFrame.this.setVisible(false);
				}
				else
				{
					errorLabel.setForeground(Color.RED);
					if(password.isEmpty())
					{
						errorLabel.setText("Please Enter Password.");
					}
					else if(username.isEmpty())
					{
						errorLabel.setText("Please Enter Username.");
					}
					else if (!valid){
						errorLabel.setText("Invalid User.");
					}	
				}
			}
		}

		public void keyReleased(KeyEvent e) {}
	}
	
	public static void main(String[] args)
	{
		LoginInfoFrame frame = new LoginInfoFrame();
		frame.setVisible(true);	
	}
}
